//
//  YBInviteEarnView.m
//  Nectar
//
//  Created by famer on 2023/11/10.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBInviteEarnView.h"
#import "DashedLineView.h"
#import "RevDataGetInviteFriendEarn.h"

@implementation YBInviteEarnView


- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        
        [self addSubview:self.invitationRewardRatioLabel];
        [self addSubview:self.invitationRewardRatioValueLabel];
        
        [self addSubview:self.invitationRewardLabel];
        [self addSubview:self.invitationRewardValueLabel];
        [self addSubview:self.invitationRewardUnitLabel];
        
        [self addSubview:self.splitLineView];
        
        [self addSubview:self.inviteNumberLabel];
        [self addSubview:self.inviteNumberValueLabel];
        
        [self addSubview:self.friendEarnLabel];
        [self addSubview:self.friendEarnValueLabel];
        [self addSubview:self.friendEarnUnitLabel];
        
        [self addSubview:self.actionButton];

        [self addSubview:self.withdrawLabel];
        [self addSubview:self.withdrawButton];
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat width = self.frame.size.width;
    CGFloat leftMargin = 12;
    CGFloat topMargin = 16;
    
    ///
    CGSize invitationRewardRatioLabelSize = [self.invitationRewardRatioLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.invitationRewardRatioLabel.frame = CGRectMake(leftMargin,
                                                       topMargin,
                                                       invitationRewardRatioLabelSize.width,
                                                       invitationRewardRatioLabelSize.height);
    
    CGSize invitationRewardRatioValueLabelSize = [self.invitationRewardRatioValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.invitationRewardRatioValueLabel.frame = CGRectMake(self.invitationRewardRatioLabel.right+10,
                                                            self.invitationRewardRatioLabel.top,
                                                            invitationRewardRatioValueLabelSize.width,
                                                            invitationRewardRatioValueLabelSize.height);
    
    self.invitationRewardRatioLabel.centerY = self.invitationRewardRatioValueLabel.centerY;
    
    ///
    self.invitationRewardLabel.frame = CGRectMake(self.invitationRewardRatioLabel.left,
                                                  self.invitationRewardRatioValueLabel.bottom+10,
                                                  invitationRewardRatioLabelSize.width,
                                                  invitationRewardRatioLabelSize.height);
    
    CGSize invitationRewardValueLabelSize = [self.invitationRewardValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.invitationRewardValueLabel.frame = CGRectMake(self.invitationRewardRatioValueLabel.left,
                                                       self.invitationRewardLabel.top,
                                                       invitationRewardValueLabelSize.width,
                                                       invitationRewardValueLabelSize.height);
    
    CGSize invitationRewardUnitLabelSize = [self.invitationRewardUnitLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.invitationRewardUnitLabel.frame = CGRectMake(self.invitationRewardValueLabel.right,
                                                      self.invitationRewardValueLabel.top,
                                                      invitationRewardUnitLabelSize.width,
                                                      invitationRewardUnitLabelSize.height);
    
    self.invitationRewardLabel.centerY = self.invitationRewardValueLabel.centerY;
    self.invitationRewardUnitLabel.centerY = self.invitationRewardValueLabel.centerY;
    
    ///
    self.splitLineView.frame = CGRectMake(leftMargin,
                                          self.invitationRewardValueLabel.bottom+16,
                                          width-leftMargin*2,
                                          4);
    
    
    ///
    self.inviteNumberLabel.frame = CGRectMake(self.invitationRewardRatioLabel.left,
                                              self.splitLineView.bottom+16,
                                              invitationRewardRatioLabelSize.width,
                                              invitationRewardRatioLabelSize.height);
    
    CGSize inviteNumberValueLabelSize = [self.inviteNumberValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.inviteNumberValueLabel.frame = CGRectMake(self.invitationRewardRatioValueLabel.left,
                                                   self.inviteNumberLabel.top,
                                                   inviteNumberValueLabelSize.width,
                                                   inviteNumberValueLabelSize.height);
    
    self.inviteNumberLabel.centerY = self.inviteNumberValueLabel.centerY;
    
    
    ///
    self.friendEarnLabel.frame = CGRectMake(self.invitationRewardRatioLabel.left,
                                            self.inviteNumberValueLabel.bottom+10,
                                            invitationRewardRatioLabelSize.width,
                                            invitationRewardRatioLabelSize.height);
    
    CGSize friendEarnValueLabelSize = [self.friendEarnValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.friendEarnValueLabel.frame = CGRectMake(self.invitationRewardRatioValueLabel.left,
                                                 self.friendEarnLabel.top,
                                                 friendEarnValueLabelSize.width,
                                                 friendEarnValueLabelSize.height);
    
    CGSize friendEarnUnitLabelSize = [self.friendEarnUnitLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.friendEarnUnitLabel.frame = CGRectMake(self.friendEarnValueLabel.right,
                                                self.friendEarnValueLabel.top,
                                                friendEarnUnitLabelSize.width,
                                                friendEarnUnitLabelSize.height);
    
    self.friendEarnLabel.centerY = self.friendEarnValueLabel.centerY;
    self.friendEarnUnitLabel.centerY = self.friendEarnValueLabel.centerY;
    
    ///
    CGSize withdrawLabelSize = [self.withdrawLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.withdrawLabel.frame = CGRectMake(width-leftMargin-withdrawLabelSize.width,
                                          topMargin,
                                          withdrawLabelSize.width,
                                          withdrawLabelSize.height);
    
    self.withdrawButton.frame = CGRectMake(self.withdrawLabel.left,
                                           self.withdrawLabel.top,
                                           self.withdrawLabel.width,
                                           self.withdrawLabel.height);
    
    self.actionButton.frame = self.bounds;
    
    [self updateUI];
}

- (void)updateUI{
    self.clipsToBounds = YES;
    self.layer.cornerRadius = 10;
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    UIColor *endColor = [UIColor hexColor:@"4CA458"];
    UIColor *startColor = [UIColor hexColor:@"70CB77"];
    gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
    
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.layer insertSublayer:gradientLayer atIndex:0];
    
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:self.withdrawLabel.text];
    [title addAttribute:NSUnderlineStyleAttributeName
                  value:@(NSUnderlineStyleSingle)
                  range:(NSRange){0,[title length]}];
    [title addAttribute:NSUnderlineColorAttributeName value:[UIColor hexColor:@"FFFFFF"] range:(NSRange){0,[title length]}];
    [self.withdrawLabel setAttributedText:title];
}

- (CGFloat)getViewHeightFromSpecialWidth:(CGFloat)width{
    
    CGFloat leftMargin = 12;
    CGFloat topMargin = 16;
    
    CGFloat totalHeight = 0;
    
    // invitationRewardRatioValueLabelSize
    CGSize invitationRewardRatioValueLabelSize = [self.invitationRewardRatioValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=topMargin;
    totalHeight+=invitationRewardRatioValueLabelSize.height;
    
    // invitationRewardValueLabelSize
    CGSize invitationRewardValueLabelSize = [self.invitationRewardValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=10;
    totalHeight+=invitationRewardValueLabelSize.height;
    
    // line
    totalHeight+=16;
    totalHeight+=4;
    
    ///
    CGSize inviteNumberValueLabelSize = [self.inviteNumberValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=16;
    totalHeight+=inviteNumberValueLabelSize.height;
    
    ///
    CGSize friendEarnValueLabelSize = [self.friendEarnValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=10;
    totalHeight+=friendEarnValueLabelSize.height;
    
    ///
    totalHeight+=topMargin;
    
    return totalHeight;
}

- (void)toWithdraw{
    if(self.withdrawAction){
        self.withdrawAction();
    }
}
- (void)toDetail{
    if(self.toDetailAction){
        self.toDetailAction();
    }
}

- (void)setRealData:(RevDataGetInviteFriendEarn *)realData{
    if(realData!=nil){
        self.invitationRewardRatioValueLabel.text = [NSString stringWithFormat:@"%@", [realData.incomeRate toFinancePercentString]];
        
        self.invitationRewardValueLabel.text = [NSString stringWithFormat:@"%@", realData.myIncome];
    
        self.inviteNumberValueLabel.text = [NSString stringWithFormat:@"%@", realData.inviteNum];
        self.friendEarnValueLabel.text = [NSString stringWithFormat:@"%@", realData.friendIncome];
        
        [self setNeedsLayout];
        [self layoutIfNeeded];
    }
}

#pragma mark - Getter Setter

YBLabelGetter(invitationRewardRatioLabel, _invitationRewardRatioLabel, [UIFont systemFontOfSize:12], @"Invitation rewards ratio", kWhiteColor, NSTextAlignmentLeft);
YBLabelGetter(invitationRewardRatioValueLabel, _invitationRewardRatioValueLabel, [UIFont boldSystemFontOfSize:17], @"*%", kWhiteColor, NSTextAlignmentLeft);

YBLabelGetter(invitationRewardLabel, _invitationRewardLabel, [UIFont systemFontOfSize:12], @"Invitation rewards", kWhiteColor, NSTextAlignmentLeft);
YBLabelGetter(invitationRewardValueLabel, _invitationRewardValueLabel, [UIFont boldSystemFontOfSize:17], @"*%", kWhiteColor, NSTextAlignmentLeft);
YBLabelGetter(invitationRewardUnitLabel, _invitationRewardUnitLabel, [UIFont systemFontOfSize:10], @"USDT", kWhiteColor, NSTextAlignmentLeft);

YBLabelGetter(inviteNumberLabel, _inviteNumberLabel, [UIFont systemFontOfSize:12], @"Invited number", kWhiteColor, NSTextAlignmentLeft);
YBLabelGetter(inviteNumberValueLabel, _inviteNumberValueLabel, [UIFont boldSystemFontOfSize:17], @"*", kWhiteColor, NSTextAlignmentLeft);

YBLabelGetter(friendEarnLabel, _friendEarnLabel, [UIFont systemFontOfSize:12], @"Friends earn", kWhiteColor, NSTextAlignmentLeft);
YBLabelGetter(friendEarnValueLabel, _friendEarnValueLabel, [UIFont boldSystemFontOfSize:17], @"*", kWhiteColor, NSTextAlignmentLeft);
YBLabelGetter(friendEarnUnitLabel, _friendEarnUnitLabel, [UIFont systemFontOfSize:10], @"USDT", kWhiteColor, NSTextAlignmentLeft);

YBLabelGetter(withdrawLabel, _withdrawLabel, [UIFont boldSystemFontOfSize:18], @"Withdraw", kWhiteColor, NSTextAlignmentCenter);

- (DashedLineView *)splitLineView{
    if(!_splitLineView){
        _splitLineView = [[DashedLineView alloc] initWithFrame:CGRectZero];
    }
    return  _splitLineView;
}

- (UIButton *)withdrawButton{
    if(!_withdrawButton){
        _withdrawButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _withdrawButton.backgroundColor = kClearColor;
        [_withdrawButton setTitle:@"" forState:UIControlStateNormal];
        [_withdrawButton setTitle:@"" forState:UIControlStateSelected];
        [_withdrawButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
        [_withdrawButton setTitleColor:kWhiteColor forState:UIControlStateSelected];
        [_withdrawButton addTarget:self action:@selector(toWithdraw) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _withdrawButton;
}


- (UIButton *)actionButton{
    if(!_actionButton){
        _actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _actionButton.backgroundColor = kClearColor;
        [_actionButton setTitle:@"" forState:UIControlStateNormal];
        [_actionButton setTitle:@"" forState:UIControlStateSelected];
        [_actionButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
        [_actionButton setTitleColor:kWhiteColor forState:UIControlStateSelected];
        [_actionButton addTarget:self action:@selector(toDetail) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _actionButton;
}
@end
